#ifndef ASIO_SERVER_SINGLETON_H
#define ASIO_SERVER_SINGLETON_H

#include <memory>
#include <mutex>
#include <iostream>

template<typename T>
class Singleton {
public:
    static std::shared_ptr<T> GetInstance() {
        static std::once_flag s_flag;
        std::call_once(s_flag, [&] {
            _instance = std::shared_ptr<T>(new T);
        });

        return _instance;
    }

    ~Singleton() {
        std::clog << "单例析构" << std::endl;
    }

    Singleton() = default;

    Singleton(const Singleton<T> &) = delete;

    Singleton &operator=(const Singleton<T> &) = delete;

protected:
    static std::shared_ptr<T> _instance;
};


template<typename T>
std::shared_ptr<T> Singleton<T>::_instance = nullptr;

#endif //ASIO_SERVER_SINGLETON_H
